package com.dididoctor.doctor.Activity.Order;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dididoctor.doctor.Activity.PatientDetial.PatientDetialTHActivity;
import com.dididoctor.doctor.MV.adapter.CommonAdapter;
import com.dididoctor.doctor.MV.adapter.ViewHolder;
import com.dididoctor.doctor.R;
import com.dididoctor.doctor.Utils.LoaderImage;
import com.dididoctor.doctor.Utils.SharedPreferencesUtils;
import com.dididoctor.doctor.Utils.UserHeadImage;
import com.dididoctor.doctor.Utils.Util;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import java.util.List;

/**
 * @version 2.0
 * @类描述：
 * @项目名称：ddgh
 * @类名称： OrderAdapter
 * @包名称： com.dididoctor.doctor.Activity.Order
 * @创建人： zhanjunjie
 * @创建时间：2016/4/20 18:43
 * @修改人：
 * @修改时间：2016/4/20 18:43
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class OrderAdapter extends CommonAdapter<OrderBean> {

    private TextView mTvName, mTvAge, mTvPhoneOrText, mTvStatus, mTvCombo, mTvTime;
    private UserHeadImage headImage;
    private LoaderImage loaderImage;
    private Context context;
    private ImageView mImgSex, mImgMessageDot;
    private LinearLayout mLlCombo, mLlAll;

    public OrderAdapter(Context context, List<OrderBean> datas) {
        super(context, datas, R.layout.item_order);
        this.context = context;
        loaderImage = new LoaderImage(context);
    }

    @Override
    public void convert(final int position, ViewHolder helper, final OrderBean item) {
        headImage = helper.getView(R.id.img_userimg);
        mLlAll = helper.getView(R.id.lin_all);
        loaderImage.load(headImage, item.getHeadPic(), new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String s, View view) {
                headImage.setImageResource(R.drawable.img_doctordefault);
            }

            @Override
            public void onLoadingFailed(String s, View view, FailReason failReason) {
                headImage.setImageResource(R.drawable.img_doctordefault);
            }

            @Override
            public void onLoadingComplete(String s, View view, Bitmap bitmap) {

            }

            @Override
            public void onLoadingCancelled(String s, View view) {

            }
        });
        //时间
        mTvTime = helper.getView(R.id.tv_time);
        mTvTime.setText(item.getStartTime());
        //姓名
        mTvName = helper.getView(R.id.tv_name);
        mTvName.setText(item.getName());
        //性别
        mImgSex = helper.getView(R.id.img_sex);
        if ("0".equals(item.getSex())) {
            mImgSex.setBackground(context.getResources().getDrawable(R.drawable.icon_girl));
        } else {
            mImgSex.setBackground(context.getResources().getDrawable(R.drawable.icon_boy));
        }
        //年龄
        mTvAge = helper.getView(R.id.tv_age);
        mTvAge.setText("年龄:" + item.getAge());
        //是预约电话还是图文资讯
        mTvPhoneOrText = helper.getView(R.id.tv_phone_call);
//        1-即时咨询 2-电话咨询
        if ("1".equals(item.getType())) {
            mTvPhoneOrText.setText("图文咨询");
        } else {
            mTvPhoneOrText.setText("预约电话");
        }
        //订单状态 1-未处理 2-进行中 3-已完结 4-已过期 5-已退款

        mTvStatus = helper.getView(R.id.tv_status);
        if ("1".equals(item.getStatus())) {
            mTvStatus.setVisibility(View.VISIBLE);
            mTvStatus.setText("未处理");
        } else if ("2".equals(item.getStatus())) {
            mTvStatus.setVisibility(View.VISIBLE);
            mTvStatus.setText("进行中");
        } else if ("3".equals(item.getStatus())) {
            mTvStatus.setVisibility(View.VISIBLE);
            mTvStatus.setText("已完结");
        } else if ("4".equals(item.getStatus())) {
            mTvStatus.setVisibility(View.VISIBLE);
            mTvStatus.setText("已过期");
        } else if ("5".equals(item.getStatus())) {
            mTvStatus.setVisibility(View.VISIBLE);
            mTvStatus.setText("已退款");
        }
        //红点的显示控制  图文咨询订单状态是1,和2时，有未读的消息数时显示红点  ，没有未读的消息数时不显示  3,4,5状态下的订单都不显示红点
        //              电话咨询 订单状态是1,2的就显示红点  3,4,5就不显示红点
        mImgMessageDot = helper.getView(R.id.img_message_dot);
//        if ("1".equals(item.getType())) {
        if ("1".equals(item.getStatus())) {
            if ("0".equals(item.getChatstatus())) {
                mImgMessageDot.setVisibility(View.VISIBLE);
            } else {
                mImgMessageDot.setVisibility(View.GONE);
            }
        } else if ("2".equals(item.getStatus())) {
            if ("0".equals(item.getChatstatus())) {
                mImgMessageDot.setVisibility(View.VISIBLE);
            } else {
                mImgMessageDot.setVisibility(View.GONE);
            }
        } else {
            mImgMessageDot.setVisibility(View.GONE);
        }
//        }
//        else {
//            if ("1".equals(item.getStatus())) {
//                    mImgMessageDot.setVisibility(View.VISIBLE);
//            } else if ("2".equals(item.getStatus())) {
//                    mImgMessageDot.setVisibility(View.VISIBLE);
//            } else {
//                mImgMessageDot.setVisibility(View.GONE);
//            }
//        }

        //套餐
        mLlCombo = helper.getView(R.id.lin_combo);
        mTvCombo = helper.getView(R.id.tv_combo);
        if (Util.isEmpty(item.getOrderpackage())) {
            mLlCombo.setVisibility(View.GONE);
        } else {
            mLlCombo.setVisibility(View.VISIBLE);
            mTvCombo.setText(item.getOrderpackage());
        }
        mLlAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferencesUtils.setParam(context, "UpDataOrderStatus", item.getOrderId());
//
                if ("1".equals(item.getType())) {
//                    mTvPhoneOrText.setText("图文咨询");
                    Intent intent = new Intent(context, PatientDetialTHActivity.class);
                    intent.putExtra("clientId", item.getClientId());
                    intent.putExtra("status", item.getStatus());
                    intent.putExtra("isSign", item.getIsSign());
                    intent.putExtra("accId", item.getAccId());
                    intent.putExtra("isCallorpictury", item.getType());
                    context.startActivity(intent);

                } else {
                    Intent intent = new Intent(context, PatientDetialTHActivity.class);
                    intent.putExtra("clientId", item.getClientId());
                    intent.putExtra("status", item.getStatus());
                    intent.putExtra("isSign", item.getIsSign());
                    intent.putExtra("accId", item.getAccId());
                    intent.putExtra("isCallorpictury", item.getType());
                    context.startActivity(intent);
                }
            }
        });
    }
}
